<?php
 global $objTemplate, $arrConfig,$sCanEdit,$lang;
  $sCanEdit = 1;  
  $formTitle = 'quan ly Menu'; 
 $strCheck = createTblHTMLPower(4);
  # start region process data 
  initPage();

     $objXajax->registerFunction("loadGrid");
     function initPage(){
      global $maxorder,$father;
      $maxorder = getMaxOrder();
      $father   = createOptionFather();
            
  }
  
  function createOptionFather(){
    global $objDbSelect,$lang;
      $sql = "SELECT id AS id, name_$lang"."_menu as name from tbl_menu WHERE category_type =1 order by category_order ";
     
      $arrfather = $objDbSelect->GetArray($sql);
      $temp ='';
      $count = count($arrfather);
      for($i=0;$i<$count;$i++){
        $id = $arrfather[$i]['id'];
        $name = $arrfather[$i]['name'];
        $temp.= "<option value='$id'> $name </option>"  ;
      }
     // echo $temp;die();
      return $temp;
  }
  function getMaxOrder(){
      global $objDbSelect;
      $sql = 'SELECT MAX(category_order) AS maxorder from tbl_menu';
      $arrOrder = $objDbSelect->GetArray($sql);
      if(count($arrOrder)>0){
          return $arrOrder[0]['maxorder'] + 1;
      }
      return 1;
  }
  
  function getOrderByFather($father_id){
      global $objDbSelect;
      $sql = "SELECT category_order AS cateorder from tbl_menu WHERE id = $father_id";
      $arrOrder = $objDbSelect->GetArray($sql);
      if(count($arrOrder)>0){
          return $arrOrder[0]['cateorder'] ;
      }
      return 1;
  }
  
  function createTblHTMLPower($numCol){
      global $objDbSelect;
      $lang = $_SESSION['lang'];
      $sql = "select id,name_$lang as name from tbl_power_type";

      $arrMenu = $objDbSelect->GetArray($sql);
          
      $count = count($arrMenu);
      $numadd = ($numCol- $count%$numCol)%$numCol;
      for($j=0;$j<$numadd;$j++){
          $arrPustemp= array();
          $arrPustemp[$count+$j-1] = array('id'=>0,'name'=>'');
          $arrMenu=array_merge($arrMenu,$arrPustemp);
      }
      $count = count($arrMenu);
      $tbl ="<table id='tblpower' style='padding-left: 20px'>";
      for($i=0;$i<$count;$i++){
          $id = $arrMenu[$i]['id'];
          $name = $arrMenu[$i]['name'];
          $tdTemp='';
          if($id!=0){
           $tdTemp = "<td><input type='checkbox'  class='chkbox' onchange=\"fn_SubChkChange('chkAllPower','chkpower')\" checked='checked'  id='chkpower$id' value='$id' name='chkpower'  /> <b>$name</b></td>";   
          }else{
             $tdTemp ='<td></td>'; 
          }
          
          if($i%$numCol==0){
            $tbl .= "<tr>$tdTemp";  
          }else if($i%$numCol==$numCol-1){
            $tbl .= "$tdTemp</tr>";  
          }else{
             $tbl .= "$tdTemp"; 
          }
      }
      return $tbl.'</table>';
  }
  
  function SaveFormCus($params){
      global $objDbUpdate;
      $objResponse = new xajaxResponse();
      if($params['cmbCategoryType']==1){
         $params['category_order'] = getMaxOrder();
         $params['cmbFather'] = 0;
      }else{
         $params['category_order'] = getOrderByFather($params['cmbFather']);         
      }
      SaveFormNormal($params);
      SavePowerTypeMenu($params);
      $objResponse->addScript('xajax_loadGrid();');
      return    $objResponse->getXML();       
  }
  function SavePowerTypeMenu($params){
      global $objDbUpdate,$objDbSelect;
      $sqlSelect ='select id,category_type from tbl_menu where id = (select max(id) from tbl_menu)';
    // echo  $sqlSelect; die();
      $arr = $objDbSelect->GetArray($sqlSelect);
      $menu_id = $arr[0]['id'];
      $category_type = $arr[0]['category_type'];
      $str ='';
      foreach($params as $key=>$value){
          if(substr($key,0,8)=='chkpower'&&$value==1){
             $str.=',('.substr($key,8).",$menu_id,$category_type)";
          }
      }
      if($str==''){
          return '';
      
      }
      $str= substr($str,1); 
      $sqlupdate = 'insert into tbl_menu_power(power_id,menu_id,category) values'.$str;
      $objDbUpdate->Execute($sqlupdate);
      return;
      
  }
  
  function loadGrid()
{     
    global $lang,$objDbSelect;
    $lang ='vi';
    $arrMenu = GetMenu();
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/main_menu_grid.html');
    $oTbs->MergeBlock('blk_data', $arrMenu);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('divGrid', 'innerHTML', $oTbs->Source); 
    $objResponse->addScriptCall('initTable();');                              
    return $objResponse->getXML();
}

function  GetMenu(){
    global $objDbSelect;
    $sqlSelect = 'select *,if(category_type=1,"Category","Menu item") as category, if(help=1,"x","") as flghelp, if(active=1,"icon-active","icon-deactivated") as image,if(active=1,"Ngừng kích hoạt","Kích hoạt") as title from tbl_menu order by category_order,id';
   // echo $sqlSelect; die();
    return $objDbSelect->GetArray($sqlSelect);
}
  
  # end region process data
?>
